package fr.demos.cinepantin.controleur;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.demos.cinepantin.Article;
import fr.demos.cinepantin.Livre;
import fr.demos.cinepantin.dao.DaoArticle;
import fr.demos.cinepantin.dao.DaoLivre;
import fr.demos.cinepantin.dao.interfac.ArticleDao;
import fr.demos.cinepantin.dao.interfac.LivreDao;
import fr.demos.cinepantin.exception.DaoExceptions;

/**
 * @author CQP-DNT
 * Servlet implementation class AfficherLivre
 */
@WebServlet("/AfficherLivre")
public class AfficherLivre extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Inject
	@DaoLivre
	private LivreDao livredao;

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AfficherLivre() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		HttpSession session = request.getSession();
		RequestDispatcher rd;

		List<Livre> livre = new ArrayList<>();
		// récupération des livres en session

		try {
			livre = livredao.getAllLivre();
			request.setAttribute("listeLivres", livre);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("erreurPage", "Livre indisponible");
		}

		request.setAttribute("page", "/afficherLivre.jsp");
		rd = request.getRequestDispatcher("/page.jsp");
		rd.forward(request, response);

		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// doGet(request,response);
		
		String titre = (String) request.getParameter("titre");
		ArrayList<Livre> listeLivres = null;
		try {
			listeLivres = (ArrayList<Livre>)livredao.getLivreByTitre(titre);
			request.setAttribute("listeLivres", listeLivres);
		} catch (DaoExceptions e) {
			e.printStackTrace();
		}
		request.setAttribute("page","afficherLivre.jsp");
		request.getRequestDispatcher("page.jsp").forward(request, response);
		
	}
	}


